/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.mabi.frontend.appbuilder;

import com.mabi.modular.MabiConstants;
import com.mabi.modular.admin.modules.IModuleDefinition;
import com.mabi.modular.admin.modules.ModuleDefinition;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;

/**
 *
 * @author User
 */
public class ModuleLoader {

    List<IModuleDefinition> modules = null;

    public List<IModuleDefinition> loadModules() throws NamingException {
        lookupModules();
        for (IModuleDefinition iModuleDefinition : modules) {
            System.out.println("THE MODULE NAME : " + iModuleDefinition.getModuleName());
        }
        return modules;
    }

    private void lookupModules() throws NamingException {
        String[] registeredModules = {"Permisos","AdmFactores"};
        if (null == modules) {
            modules = new ArrayList();
        }
        Context c = new InitialContext();
        for (String regmod : registeredModules) {
            String lookupString = MabiConstants.MODULE_LOOKUP_PREFIX + regmod + "/" + MabiConstants.MODULE_FILE_NAME;
            try {
                modules.add((IModuleDefinition) c.lookup(lookupString));
            } catch (NamingException ex) {
                Logger.getLogger(ModuleLoader.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }
}
